:- dynamic(card/9).
:- dynamic(card1/10).
:- dynamic(deck/3).

% card(Num, CardName, Cost, Str, Type, Rules, FlavorText, Rarity, Artist)

% file location
file(cards, csv, 'netrunner/netrunner cards.csv').
file(cards, base, 'netrunner/nrbase.pl').
file(cards, base1, 'netrunner/nrbase1.pl').
file(deck, base, 'netrunner/deck.pl').

% load main files: cards description, desk
load:-file(cards, base1, FCards),
	file(deck, base, FBase),
	[FCards, FBase].


% read csv information
read_cards:-file(cards, csv, F),
	csv_read_file(F, RowList, [ separator(0';)]),
	length(RowList, L),
	writeln(L),
	abolish(card,9),
	perform_cl(RowList),
	!.

% export card as base
write_cards:-
	file(cards, base1, F),
	tell(F),
	forall( card(N, Nm, Cs, St, Tp, Ru, Ft, Rr, Ar),
		(writeq( card(N, Nm, Cs, St, Tp, Ru, Ft, Rr, Ar)), write('.'), nl)
	      ),
	told.

write_deck:-
	file(deck, base, F),
	tell(F),
	forall( deck(Deck, Name, N),
		(writeq(deck(Deck, Name, N)), write('.'), nl)
	      ),
	told.

%write_cards1:-
%	file(cards, base1, F),
%	write_terms(F, card(N, Nm, Cs, St, Tp, Ru, Ft, Rr, Ar)).
%
%write_terms(F, T):- tell(F),
%	forall( T,
%		(writeq( T), write('.'), nl)
%	      ),
%	told.


perform_cl(RowList):- perform_cl01(0,RowList),!.

perform_cl01(_,[]).
perform_cl01(Num,[row('', _, _, _, _, Rules2, _, _, _)|T]):-
	retract(card(Num, CardName, Cost, Str, Type, Rules1, FlavorText, Rarity, Artist)),
	append(Rules1,[Rules2], Rules),
	assert(card(Num, CardName, Cost, Str, Type, Rules, FlavorText, Rarity, Artist)),
	!,
	perform_cl01(Num,T).
perform_cl01(_,[row(No, CardName, Cost, Str, Type, Rules, FlavorText, Rarity, Artist)|T]):-
	No \= '',
	assert(card(No, CardName, Cost, Str, Type, [Rules], FlavorText, Rarity, Artist)),
	!,
	perform_cl01(No,T).


set_abbr:- forall( card(N, Nm, Cs, St, Tp, Ru, Ft, Rr, Ar),
		     (writeln(Nm),read(Abbr),assert(card1(N, Abbr, Nm, Cs, St, Tp, Ru, Ft, Rr, Ar)))
	      ).

% Deck = corp|nrun; Action = add|del ; Abbr = Short Name ; Quan =
% Quantity
% move(Deck, Action, Num, Abbr)

move(Deck, add, Num, Abbr):- abbr_uniq(Abbr, CardName),
	add_card(Deck, CardName, Num),!.

abbr_uniq(Abbr, L):-
	findall(CardName,
		( card(_, CardName, _, _, _, _, _, _, _),
		  search1(Abbr, CardName)
		),
		L),
	length(L,N),!,
 	abbr_uniq01(L,N).

abbr_uniq01([H],1):- writef('determ - <%w>',[H]).
abbr_uniq01(L,_):- writef(  'not uniq - <%p>',[L]), fail.

add_card(Deck, [H], N):-assert(deck(Deck,H,N)).


search(Name):- card(Num, CardName, Cost, Str, Type, Rules, FlavorText, Rarity, Artist),
	upcase_atom(Name, UName),
	upcase_atom(CardName, UCardName),
	sub_atom(UCardName,0, _, _, UName),
	write(CardName).

search1(Name, CardName):-
	upcase_atom(Name, UName),
	upcase_atom(CardName, UCardName),
	sub_atom(UCardName,0, _, _, UName).

show_carda(Abbr):- abbr_uniq(Abbr, [CardName]),
	show_card(CardName).

show_card(CardName):-card(Num, CardName, Cost, Str, Type, Rules, FlavorText, Rarity, Artist),
	nl, nl,
	writef('Name:	  %w\n',[CardName]),
	writef('Number:	  %w\n',[Num]),
	writef('Type:     %w\n',[Type]),
	writef('Rarity:   %w\n',[Rarity]),
	writef('Expansion:%w\n',['...']),
	writef('Artist:   %w\n',[Artist]),
	writef('Cost:     %w\n',[Cost]),
	writef('STR:      %w\n',[Str]),
	writeln('Rules============='),
	forall(member(R,Rules), writeln(R)),
	writeln('Text=============='),
	writeln(FlavorText),
	nl, nl.

show_deck(corp):-
	show_group(corp, 'Operation'),
	show_group(corp, 'Upgrade'),
	show_group(corp, 'Node'),
	show_group(corp, 'Agenda'),
	show_group(corp, 'Ice').
show_deck(nr):-
	show_group(nr, 'Prep'),
	show_group(nr, 'Program'),
	show_group(nr, 'Hardware'),
	show_group(nr, 'Resource').


show_group(Player, Category):-
	write('\t'), writeln(Category),
	findall(CardName, (deck(Player, CardName, _),
			   card(_,CardName, _, _, Type, _, _, _, _),
			   sub_atom(Type, 0, _, _, Category)
			  ), L),
	forall(member(CN, L), writeln(CN)).









